<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/admin/AdminAction.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/Response.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/Service.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Person.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Pattern.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/utils/Str.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/ServiceFactory.php';

final class PersonPasswordSetAction extends AdminAction {
	protected function execute(Response $response) {
		$person = null;
		if(isset($_POST['identity']) && isset($_POST['password'])) {
			$person = new Person();
			$person->setIdentity($_POST['identity']);
			$person->setPassword(md5($_POST['password']));
			$serviceFactory = new ServiceFactory();
			$service = $serviceFactory->getService('PersonService');
			$service->changePassword($person);
		}
		if($person !== null) {
			$_SESSION['person-identity'] = $person->getIdentity();
			$response->setActionPath('/person/edit.html');
		} else {
			$response->setActionPath('/person/index.html');
		}
	}
}
?>